package com.emuporium.gymrat.server.routine;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.emuporium.gymrat.server.data.ExerciseDB;
import com.emuporium.gymrat.server.data.Routine;
import com.emuporium.gymrat.server.data.RoutineDB;
import com.emuporium.gymrat.server.data.User;
import com.emuporium.gymrat.server.data.UserDB;

@SuppressWarnings("serial")
public class RoutineSearchServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String descSearch = request.getParameter("description");
		if (descSearch != null)
			request.setAttribute("description", descSearch);
		LinkedHashMap<Integer, String> groups = ExerciseDB.selectMuscleGroups();
		request.setAttribute("groups", groups);
		
		User user = UserDB.selectUser(request.getRemoteUser());
		ArrayList<Routine> results = RoutineDB.searchRoutines(null, user.getId());
		
		request.setAttribute("results", results);

		String url = "/routine/search.jsp";
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(url);
		dispatcher.forward(request, response);
	}

}
